package org.footito.android.ui.activity;

import greendroid.app.GDExpandableListActivity;
import greendroid.widget.ActionBarItem;
import greendroid.widget.LoaderActionBarItem;

import org.footito.android.R;
import org.footito.android.data.update.UpdateService;
import org.footito.android.data.update.UpdateService.LocalBinder;
import org.footito.android.data.update.UpdateTaskListener;
import org.footito.android.tools.Log;
import org.footito.android.tools.Settings;
import org.footito.android.ui.adapters.TimelineAdapter;
import org.footito.android.ui.notifications.NotificationsManager;
import org.footito.android.ui.views.TeamSelectorDialog;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class Timeline extends GDExpandableListActivity implements ServiceConnection, UpdateTaskListener {

	private static final int DIALOG_FILTER = 0;
	public static boolean isRunning = false;
	private UpdateService updateService;
	private final Handler mHandler = new Handler();
	private LoaderActionBarItem loaderItem;
	private NotificationsManager nm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		UpdateService.bindService(this, this);

		// setContentView(R.layout.simplelist);

		setAdapter();
		// getActionBar().setType(Type.Dashboard);
		getExpandableListView().setGroupIndicator(null);

		loaderItem = (LoaderActionBarItem) addActionBarItem(ActionBarItem.Type.Refresh, R.id.action_bar_refresh);
		
		nm = new NotificationsManager(this);
	}

	private void setAdapter() {
		setListAdapter(new TimelineAdapter(this, getExpandableListView()));
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		Timeline.isRunning = true;

		int position = Settings.getTimelinePosition(this);
		getExpandableListView().setSelection(position);
		
		nm.removeNotifications(this);
	}

	@Override
	protected void onPause() {
		Settings.setTimelinePosition(this, getExpandableListView().getFirstVisiblePosition());
		Timeline.isRunning = false;
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		UpdateService.unbindService(this, this);
		super.onDestroy();
	}

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		Log.d("Service binded");
		this.updateService = ((LocalBinder) service).getService();
		updateService.addListener(this);
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		this.updateService = null;
	}

	@Override
	public void onComplete(int statusAdded) {
		((TimelineAdapter) getExpandableListAdapter()).reloadStatus(this);
		// getExpandableListView().invalidateViews();
		mHandler.postDelayed(new Runnable() {
			public void run() {
				loaderItem.setLoading(false);
			}
		}, 2000);
	}

	@Override
	public void onError() {
		mHandler.postDelayed(new Runnable() {
			public void run() {
				loaderItem.setLoading(false);
			}
		}, 2000);
	}

	@Override
	public void onProgress(ProgressEvents event, int div) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {

		switch (item.getItemId()) {

		case R.id.action_bar_refresh:
			updateService.performUpdate(this, null);
			break;
		default:
			return super.onHandleActionBarItemClick(item, position);
		}

		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.timeline, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_top) {
			getExpandableListView().setSelection(0);
		}
		if (item.getItemId() == R.id.menu_bottom) {
			getExpandableListView().setSelection(getExpandableListAdapter().getGroupCount());
		}
		if (item.getItemId() == R.id.menu_apropos) {
			InfoTabActivity.startActivity(this);
		}
		if (item.getItemId() == R.id.menu_filter) {
			showDialog(DIALOG_FILTER);
		}
		if (item.getItemId() == R.id.menu_prefs) {
			FootitoPreferenceActity.startActivity(this);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_FILTER:
			return new TeamSelectorDialog(Timeline.this);

		default:
			return super.onCreateDialog(id);
		}
	}

}
